{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import math\n",
    "\n",
    "def welcome():\n",
    "    name = input('君の名前は:')\n",
    "    print('欢迎！')\n",
    "    return name\n",
    "def win(winner_name):\n",
    "    print('你赢了！', winner_name)\n",
    "def lose(lose_name):\n",
    "    print('你输了！', lose_name)\n",
    "def game_over(n): # 形式参数\n",
    "    print('游戏结束', n)\n",
    "def menu():\n",
    "    print('''\n",
    "    =====游戏菜单====\n",
    "    1. 游戏说明\n",
    "    2. 开始游戏\n",
    "    3. 退出游戏\n",
    "    4. 制作团队\n",
    "    ================\n",
    "    ''')\n",
    "def guess_number(user_name, di):\n",
    "    upper = int(input('上限:'))# 10 5\n",
    "    num = random.randint(1, upper)# 3\n",
    "    \n",
    "    guess_time = 0\n",
    "    time = math.ceil(math.log2(upper))\n",
    "    if di == 1:\n",
    "        max_time = time *2 \n",
    "    elif di == 2:\n",
    "        max_time = time # 4 ceiling 天花板 3.5  round 地板 3\n",
    "    \n",
    "    is_win = False\n",
    "    while guess_time < max_time:\n",
    "        guess = int(input('请输入正整数:'))\n",
    "        guess_time += 1\n",
    "        \n",
    "        print('一共可以猜', max_time, '次, 已经猜了', guess_time, '次')\n",
    "        \n",
    "        if guess == num:\n",
    "            print('你猜对了！答案是', num, '你猜了', guess_time, '次就猜对了！')#\n",
    "            win(user_name)\n",
    "            is_win = True\n",
    "            break #\n",
    "        elif guess < num:\n",
    "            print('太小了，整点儿大的')\n",
    "        elif guess > num and guess <= upper:\n",
    "            print('太大了，悠着点儿')\n",
    "        elif guess > upper:\n",
    "            print('你猜的太大了，系统打了你一下！')\n",
    "    if not is_win: # guess_time >= max_time\n",
    "        print('答案是', num)\n",
    "        lose(user_name)\n",
    "def introduce():\n",
    "    print('游戏简介，略')\n",
    "def about_us():\n",
    "    print('制作团队，略')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "君の名前は: aaa\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢迎！\n",
      "\n",
      "    =====游戏菜单====\n",
      "    1. 游戏说明\n",
      "    2. 开始游戏\n",
      "    3. 退出游戏\n",
      "    4. 制作团队\n",
      "    ================\n",
      "    \n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "你选择: 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "            请选择难度：\n",
      "            1. 简单\n",
      "            2. 困难\n",
      "            \n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "你选择: 1\n",
      "上限: 3\n",
      "请输入正整数: 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共可以猜 4 次, 已经猜了 1 次\n",
      "太大了，悠着点儿\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入正整数: 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共可以猜 4 次, 已经猜了 2 次\n",
      "太大了，悠着点儿\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入正整数: 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共可以猜 4 次, 已经猜了 3 次\n",
      "太大了，悠着点儿\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入正整数: 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共可以猜 4 次, 已经猜了 4 次\n",
      "你猜对了！答案是 1 你猜了 4 次就猜对了！\n",
      "你赢了！ aaa\n",
      "\n",
      "    =====游戏菜单====\n",
      "    1. 游戏说明\n",
      "    2. 开始游戏\n",
      "    3. 退出游戏\n",
      "    4. 制作团队\n",
      "    ================\n",
      "    \n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "你选择: 2\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "            请选择难度：\n",
      "            1. 简单\n",
      "            2. 困难\n",
      "            \n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "你选择: 2\n",
      "上限: 3\n",
      "请输入正整数: 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共可以猜 2 次, 已经猜了 1 次\n",
      "你猜对了！答案是 1 你猜了 1 次就猜对了！\n",
      "你赢了！ aaa\n",
      "\n",
      "    =====游戏菜单====\n",
      "    1. 游戏说明\n",
      "    2. 开始游戏\n",
      "    3. 退出游戏\n",
      "    4. 制作团队\n",
      "    ================\n",
      "    \n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    name = welcome()\n",
    "    \n",
    "    while True:\n",
    "        menu()\n",
    "        choice = int(input('你选择:'))\n",
    "        if choice == 1:\n",
    "            introduce()# menu()\n",
    "        elif choice == 2:\n",
    "            print('''\n",
    "            请选择难度：\n",
    "            1. 简单\n",
    "            2. 困难\n",
    "            ''')\n",
    "            di = int(input('你选择:'))\n",
    "            guess_number(name, di)\n",
    "        elif choice == 3:\n",
    "            game_over(name) # 实际参数 n = name\n",
    "            break\n",
    "        elif choice == 4:\n",
    "            about_us()\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "welcome() -> main() -> guess_number() -> win() lose() game_over()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
